import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter_chubao/config/app_config.dart';
import 'package:flutter_chubao/models/model_product.dart';
import 'package:flutter_chubao/tools/app_colors.dart';
import 'package:flutter_chubao/tools/app_image_load.dart';
import 'package:flutter_chubao/tools/app_textstyle.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_client.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_response.dart';
import 'package:flutter_chubao/widgets/app_widget_bg.dart';
import 'package:flutter_chubao/widgets/app_widget_product.dart';
import 'package:flutter_chubao/widgets/app_widget_space.dart';
// import 'package:flutter_chubao/widgets/app_widget_protocol.dart';
import 'package:flutter_chubao/widgets/app_widget_title.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'app_home_logic.dart';
import 'app_home_state.dart';

/// @description:
/// @author
/// @date: 2023-12-13 10:17:16
class AppHomePage extends StatelessWidget {
  final AppHomeLogic logic = Get.put(AppHomeLogic());
  final AppHomeState state = Get.find<AppHomeLogic>().state;

  AppHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AppWidgetBg(
        assetImage: 'icon_bg_home',
        child: GetBuilder<AppHomeLogic>(builder: (controller) {
          return SmartRefresher(
              controller: state.refreshController,
              onRefresh: () {
                state.currentPage = 1;
                logic.getHomeData();
                logic.getHomeRemmendData();
              },
              onLoading: () {
                state.currentPage++;
                // logic.getHomeData();
                logic.getHomeRemmendData();
              },
              enablePullUp: true,
              child: SingleChildScrollView(
                padding: EdgeInsets.only(
                    top: ScreenUtil().statusBarHeight,
                    left: 14.w,
                    right: 14.w,
                    bottom: 20.w),
                child: Column(
                  children: [
                    _buildLoginTop(),
                    ScreenUtil().setVerticalSpacing(8.w),
                    _buildLoginUserBg(),

                    SizedBox(
                      height: 14.w,
                    ),
                    _buildTabbar(),

                    _buildLoginOutRecommend(),
                    SizedBox(
                      height: 14.w,
                    ),
                    // _buildCompany(),
                    // _buildLogo()
                    // _buildLoginOutTop(),
                  ],
                ),
              ));
        }),
      ),
    );
  }

  Widget _buildLoginTop() {
    return SizedBox(
      height: 42.w,
      child: Row(
        children: [
          Expanded(
              child: Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              // AppImage.getIconByPath('icon_lauch_logo',
              //     color: Colors.black, height: 25.w),
              // ScreenUtil().setHorizontalSpacing(4),
              // ScreenUtil().setVerticalSpacing
              Expanded(
                  child: Text(
                '厨宝',
                style: AppTextStyle.getBoldTextStyle(
                    fontSize: 24, color: kColorTextB70, fontFamily: 'fxhTTF'),
              ))
            ],
          )),
          // GestureDetector(
          //   child: SizedBox(
          //     width: 40.w,
          //     height: 44,
          //     child: Center(
          //       child: AppImage.getIconByPath(
          //         'icon_mine_service',
          //         width: 24,
          //       ),
          //     ),
          //   ),
          // ),
          // GestureDetector(
          //   child: SizedBox(
          //     width: 40.w,
          //     height: 44,
          //     child: Center(
          //       child: AppImage.getIconByPath(
          //         'icon_mine_messages',
          //         width: 24,
          //       ),
          //     ),
          //   ),
          // )
        ],
      ),
    );
  }

  Widget _buildLoginUserBg() {
    bool canGet = (state.homeUserEntity.currentRewardVideoAdNums ?? 1) == 0;
    bool isGet =
        (state.homeUserEntity.todayRewardTaskClickTime ?? '').isNotEmpty;
    return Container(
      width: double.infinity,
      height: 160.w,
      decoration: BoxDecoration(
          image: DecorationImage(
              alignment: Alignment.bottomCenter,
              image: AppImage.getAssetImage('icon_home_login_bg'))),
      child: Stack(
        children: [
          Positioned(
              left: 177.w,
              child: Transform.rotate(
                //旋转90度
                angle: math.pi / 9,
                child: AppImage.getIconByPath('icon_robet_top', width: 70.w),
              )),
          Positioned(
              left: 20.w,
              right: 20.w,
              bottom: 0.w,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('我的积分',
                      style: AppTextStyle.getNormalTextStyle(
                          fontSize: 13, color: Colors.white)),
                  Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: 4.w,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        RichText(
                            text: TextSpan(children: [
                          TextSpan(
                            text: (state.homeUserEntity.userinfo?.mark ?? '')
                                .toString(),
                            style: AppTextStyle.getBoldTextStyle(
                                // fontFamily: kEnumFontFamily.DINBOLD,
                                color: Colors.white,
                                fontSize: 18),
                          ),
                          // TextSpan(
                          //   text: TextUtil.formatComma3(
                          //       state.indexEntity.maxQuota ??
                          //           SSConfigApp.maxQuota),
                          //   style: AppTextStyle.getBoldTextStyle(
                          //       fontFamily: kEnumFontFamily.DINBOLD,
                          //       color: Colors.white,
                          //       fontSize: 42),
                          // )
                        ])),
                        InkWell(
                          onTap: () {
                            logic.loadRewardAd();
                          },
                          child: Container(
                            // width: 88.w,
                            height: 35.w,
                            padding: EdgeInsets.symmetric(horizontal: 10.w),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(17.5.w),
                            ),
                            child: Row(
                              children: [
                                Icon(
                                  Icons.play_arrow,
                                  color: kColorAssistB6,
                                  size: 16,
                                ),
                                SizedBox(
                                  width: 4.w,
                                ),
                                Text(
                                  '免费领取积分',
                                  style: AppTextStyle.getBoldTextStyle(
                                      fontSize: 14, color: kColorAssistB6),
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '来源：${state.homeUserEntity.channel ?? ''}', //${state.indexEntity.maxApr}%
                        style: AppTextStyle.getNormalTextStyle(
                            fontSize: 13, color: Colors.white),
                      ),
                    ],
                  ),
                  AppWidgetSpace(),
                  Padding(
                      padding: EdgeInsets.symmetric(vertical: 8.w),
                      child: Row(
                        children: [
                          RichText(
                              text: TextSpan(children: [
                            TextSpan(
                              text: '今日福利:',
                              style: AppTextStyle.getNormalTextStyle(
                                  color: Colors.white, fontSize: 13),
                            ),
                            TextSpan(
                              text: isGet
                                  ? '${state.homeUserEntity.todayRewardTaskClickTime}'
                                  : '再解锁',
                              style: AppTextStyle.getNormalTextStyle(
                                  color: Colors.white, fontSize: 13),
                            ),
                            TextSpan(
                              text: isGet
                                  ? ""
                                  : (state.homeUserEntity
                                              .currentRewardVideoAdNums ??
                                          '')
                                      .toString(),
                              style: AppTextStyle.getNormalTextStyle(
                                  // fontFamily: kEnumFontFamily.DINBOLD,
                                  color: Colors.red,
                                  fontSize: 13),
                            ),
                            TextSpan(
                              text: isGet ? "" : '篇文章，领取300积分',
                              style: AppTextStyle.getNormalTextStyle(
                                  color: Colors.white, fontSize: 13),
                            ),
                          ])),
                          SizedBox(
                            width: 14.w,
                          ),
                          Visibility(
                              visible: canGet,
                              child: InkWell(
                                onTap: () async {
                                  if (canGet == true && isGet == false) {
                                    AppDioResponse response =
                                        await AppDioClient.post(
                                      '/mini/chubao/clickTodayTask',
                                    );
                                    if (response.ok) {
                                      logic.getHomeData();
                                    }
                                  }
                                  // logic.clickVideoGG();
                                },
                                child: Container(
                                    // width: 88.w,
                                    height: 20.w,
                                    padding:
                                        EdgeInsets.symmetric(horizontal: 6.w),
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                      color: isGet
                                          ? Colors.grey[200]
                                          : Colors.white,
                                      borderRadius: BorderRadius.circular(10.w),
                                    ),
                                    child: Text(
                                      isGet ? '已领取' : '领取',
                                      style: AppTextStyle.getNormalTextStyle(
                                          fontSize: 12,
                                          color: isGet
                                              ? Colors.white
                                              : kColorAssistB6),
                                    )),
                              ))
                        ],
                      ))
                ],
              ))
        ],
      ),
    );
  }

  Widget _buildTabbar() {
    return Container(
      height: 38,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.w),
          border: Border.all(color: Colors.blue, width: 1)),
      child: Row(
        children: [
          Expanded(
              child: InkWell(
            onTap: () {
              state.type = 1;
              state.currentPage = 1;
              logic.getHomeRemmendData();
              // logic.update();
            },
            child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: state.type == 1 ? Colors.blue : Colors.transparent),
              child: Text(
                '未解锁',
                style: AppTextStyle.getNormalTextStyle(
                    fontSize: 14,
                    color: state.type == 1 ? Colors.white : Colors.blue),
              ),
            ),
          )),
          Expanded(
              child: InkWell(
            onTap: () {
              state.type = 2;
              state.currentPage = 1;
              logic.getHomeRemmendData();
              // logic.update();
            },
            child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: state.type == 2 ? Colors.blue : Colors.transparent),
              child: Text(
                '已解锁',
                style: AppTextStyle.getNormalTextStyle(
                    fontSize: 14,
                    color: state.type == 2 ? Colors.white : Colors.blue),
              ),
            ),
          )),
        ],
      ),
    );
  }

  Widget _buildLoginOutRecommend() {
    //
    return Column(
      // crossAxisAlignment: CrossAxisAlignment.start,
      children: state.productList.map((e) {
        return GestureDetector(
            onTap: () {
              // if (e.isLock != true) {
              logic.lockArticle(e);
              // }
            },
            child: Container(
              margin: EdgeInsets.only(top: 10.w),
              padding: EdgeInsets.all(8.w),
              decoration: BoxDecoration(boxShadow: const [
                BoxShadow(
                    color: Color(0xffB6C8FF),
                    offset: Offset(4, 2),
                    spreadRadius: 1.5,
                    blurRadius: 5)
              ], color: Colors.white, borderRadius: BorderRadius.circular(5)),
              child: Row(
                // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          e.title ?? '',
                          style: AppTextStyle.getBoldTextStyle(
                              fontSize: 14, color: Colors.black),
                        ),
                        SizedBox(
                          height: 6.w,
                        ),
                        Row(
                          children: [
                            AppImage.getLoad(e.img, width: 14, height: 14),
                            SizedBox(
                              width: 8.w,
                            ),
                            Text(
                              '已有${e.browNum ?? ''}人浏览',
                              style: AppTextStyle.getNormalTextStyle(
                                  fontSize: 14, color: Colors.grey.shade400),
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  Visibility(
                      visible: e.isLock != true,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Icon(
                            Icons.lock,
                            color: Colors.blue[50],
                            size: 16,
                          ),
                          SizedBox(
                            height: 6.w,
                          ),
                          Text(
                            '600积分解锁',
                            style: AppTextStyle.getNormalTextStyle(
                                fontSize: 14, color: Colors.black),
                          )
                        ],
                      ))
                ],
              ),
            ));
      }).toList(),
      // children: [
      // AppWidgetTitle(
      //   title: '精选爆款',
      // ),
      // ...state.productRemmendList.map(
      //   (e) => Padding(
      //     padding: EdgeInsets.only(top: 10.w),
      //     child: AppWidgetProduct(
      //       e,
      //       showGradient: true,
      //     ),
      //   ),
      // ),
      // SizedBox(
      //   height: 10.w,
      // ),
      // AppWidgetTitle(
      //   title: '精选推荐',
      // ),
      // ...state.productList.map(
      //   (e) =>
      //       Padding(padding: EdgeInsets.only(top: 10.w), child: Container()),
      // ),
      // Wrap(
      //   spacing: 10.w,
      //   // alignment: WrapAlignment.spaceAround,
      //   runSpacing: 10.w,
      //   children: state.productList
      //       .map(
      //         (e) => Container(
      //           width: 109.w,
      //           height: 144.w,
      //           padding: EdgeInsets.only(
      //               top: 18.w, left: 5.w, right: 5.w, bottom: 15.w),
      //           decoration: BoxDecoration(
      //               color: Colors.white,
      //               borderRadius: BorderRadius.circular(12)),
      //           child: Column(
      //             crossAxisAlignment: CrossAxisAlignment.center,
      //             mainAxisAlignment: MainAxisAlignment.center,
      //             children: [
      //               Row(
      //                 mainAxisAlignment: MainAxisAlignment.center,
      //                 children: [
      //                   AppImage.getLoad(e.logo, width: 17.w),
      //                   Padding(
      //                     padding: EdgeInsets.only(right: 4.w),
      //                     child: Text(
      //                       e.name ?? '',
      //                       style: AppTextStyle.getNormalTextStyle(
      //                         fontSize: 14,
      //                       ),
      //                       overflow: TextOverflow.ellipsis,
      //                       maxLines: 1,
      //                     ),
      //                   ),
      //                 ],
      //               ),
      //               Padding(
      //                 padding: EdgeInsets.only(top: 8.w, bottom: 4.w),
      //                 child: Text(
      //                   '', // TextUtil.formatComma3(e.maxAmount ?? 10000),
      //                   style: AppTextStyle.getBoldTextStyle(
      //                       // fontFamily: kEnumFontFamily.DINBOLD,
      //                       fontSize: 21,
      //                       color: kColorAssistB6),
      //                 ),
      //               ),
      //               Text(
      //                 '最高额度(元)',
      //                 style: AppTextStyle.getNormalTextStyle(
      //                     fontSize: 14, color: kColorTextB40),
      //               ),
      //               SizedBox(
      //                 height: 8.w,
      //               ),
      //               InkWell(
      //                 onTap: () {
      //                   // Get.toNamed(SSRouteNames.SsLoanDetailPage,
      //                   //     arguments: e.goodsId ?? 0);
      //                 },
      //                 child: Container(
      //                   height: 27.w,
      //                   padding: EdgeInsets.symmetric(horizontal: 12.w),
      //                   decoration: BoxDecoration(
      //                       border:
      //                           Border.all(width: 1, color: kColorAssistB6),
      //                       borderRadius: BorderRadius.circular(13.5.w)),
      //                   child: Text(
      //                     '立即申请',
      //                     style: AppTextStyle.getNormalTextStyle(
      //                             fontSize: 12.w, color: kColorAssistB6)
      //                         .copyWith(height: 1.7),
      //                   ),
      //                 ),
      //               )
      //             ],
      //           ),
      //         ),
      //       )
      //       .toList(),
      // )
      // ],
    );
    // return Obx(() {

    // });
  }
}
